﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class System_Pages_Navigation_SiteMap : SiteFox.Base.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            this.LoadTreeView();
        }
    }

    #region ControlsEvents
    protected void imb_SiteMapNode_AddRoot_Click(object sender, ImageClickEventArgs e)
    {
        this.LoadTreeView();
        imb_SiteMapNode_Add.Visible = false;
        imb_SiteMapNode_Delete.Visible = false;
        imb_SiteMapNode_Edit.Visible = false;
        pan_Background.Visible = true;
        pan_SiteMapNode_AddOrEdit.Visible = true;
        lab_SiteMapNode_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_SiteMapNode_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
    }
    protected void imb_SiteMapNode_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_SiteMapNode_AddOrEdit.Visible = true;
        lab_SiteMapNode_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_SiteMapNode_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
    }
    protected void imb_SiteMapNode_Edit_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_SiteMapNode_AddOrEdit.Visible = true;
        lab_SiteMapNode_AddOrEdit_Title.Text = Resources.dcCommon.dc_Edit;
        lib_SiteMapNode_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        var _SiteMapNode = SiteFox.Business.Manager.SiteMapNode.GetByID(Convert.ToInt32(tv_SiteMap.SelectedNode.Value));
        ucc_SiteMapNode_AddOrEdit_Text.Value = _SiteMapNode.Title;
        ucc_SiteMapNode_AddOrEdit_Link.Value = _SiteMapNode.Url;
        ucc_SiteMapNode_AddOrEdit_ToolTip.Value = _SiteMapNode.Description;
        ddl_SiteMapNode_AddOrEdit_Target_Value.SelectedValue = _SiteMapNode.Target;
        ucc_SiteMapNode_AddOrEdit_ImagePath.Value = _SiteMapNode.ImageUrl;
        if (_SiteMapNode.Ordering != null) 
        {
            ucc_SiteMapNode_AddOrEdit_Ordering.Value = _SiteMapNode.Ordering.ToString();
        }
    }
    protected void imb_SiteMapNode_Delete_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_SiteMapNode_Delete.Visible = true;
    }

    protected void lib_SiteMapNode_AddOrEdit_Close_Click(object sender, EventArgs e)
    {
        this.ResetSiteMapNodeAddOrEdit();
        pan_SiteMapNode_AddOrEdit.Visible = false;
        pan_Background.Visible = false;
    }
    protected void lib_SiteMapNode_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        if (lab_SiteMapNode_AddOrEdit_Title.Text == Resources.dcCommon.dc_Add)
        {
            var _SiteMapNode = new SiteFox.Business.Model.SiteMapNode();
            _SiteMapNode.Title = ucc_SiteMapNode_AddOrEdit_Text.Value;
            _SiteMapNode.Url = ucc_SiteMapNode_AddOrEdit_Link.Value;
            _SiteMapNode.Description = ucc_SiteMapNode_AddOrEdit_ToolTip.Value;
            _SiteMapNode.Target = ddl_SiteMapNode_AddOrEdit_Target_Value.SelectedValue;
            _SiteMapNode.ImageUrl = ucc_SiteMapNode_AddOrEdit_ImagePath.Value;
            if (ucc_SiteMapNode_AddOrEdit_Ordering.Value != "")
            {
                _SiteMapNode.Ordering = Convert.ToInt32(ucc_SiteMapNode_AddOrEdit_Ordering.Value);
            }
            else 
            {
                _SiteMapNode.Ordering = null;
            }
            if (tv_SiteMap.SelectedNode != null) 
            {
                _SiteMapNode.ID_SiteMapNode = Convert.ToInt32(tv_SiteMap.SelectedNode.Value);
            }
            SiteFox.Business.Manager.SiteMapNode.Create(_SiteMapNode);
        }
        else if (lab_SiteMapNode_AddOrEdit_Title.Text == Resources.dcCommon.dc_Edit)
        { 
            var _SiteMapNode = SiteFox.Business.Manager.SiteMapNode.GetByID(Convert.ToInt32(tv_SiteMap.SelectedNode.Value));
            _SiteMapNode.Title = ucc_SiteMapNode_AddOrEdit_Text.Value;
            _SiteMapNode.Url = ucc_SiteMapNode_AddOrEdit_Link.Value;
            _SiteMapNode.Description = ucc_SiteMapNode_AddOrEdit_ToolTip.Value;
            _SiteMapNode.Target = ddl_SiteMapNode_AddOrEdit_Target_Value.SelectedValue;
            _SiteMapNode.ImageUrl = ucc_SiteMapNode_AddOrEdit_ImagePath.Value;
            if (ucc_SiteMapNode_AddOrEdit_Ordering.Value != "")
            {
                _SiteMapNode.Ordering = Convert.ToInt32(ucc_SiteMapNode_AddOrEdit_Ordering.Value);
            }
            else
            {
                _SiteMapNode.Ordering = null;
            }
            SiteFox.Business.Manager.SiteMapNode.Edit(_SiteMapNode);
        }
        pan_SiteMapNode_AddOrEdit.Visible = false;
        pan_Background.Visible = false;
        this.ResetSiteMapNodeAddOrEdit();
        this.LoadTreeView();
    }
    protected void lib_SiteMapNode_Delete_Close_Click(object sender, EventArgs e)
    {
        pan_SiteMapNode_Delete.Visible = true;
        pan_Background.Visible = true;
    }
    protected void lib_SiteMapNode_Delete_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Manager.SiteMapNode.Delete(Convert.ToInt32(tv_SiteMap.SelectedNode.Value));
        this.LoadTreeView();
        pan_SiteMapNode_Delete.Visible = false;
        pan_Background.Visible = false;
    }

    protected void tv_SiteMap_SelectedNodeChanged(object sender, EventArgs e)
    {
        imb_SiteMapNode_Add.Visible = true;
        imb_SiteMapNode_Delete.Visible = true;
        imb_SiteMapNode_Edit.Visible = true;
    }
    #endregion

    #region Methods
    void LoadTreeView()
    {
        tv_SiteMap.Nodes.Clear();
        var _RootNodes = SiteFox.Business.Manager.SiteMapNode.GetRootNodes();
        for (int i = 0; i < _RootNodes.Count; i++) 
        {
            var _RootNode = _RootNodes[i];
            TreeNode _tn = new TreeNode(_RootNode.Title, _RootNode.ID.ToString());
            this.LoadChildNodes(_tn);
            tv_SiteMap.Nodes.Add(_tn);
        }
        tv_SiteMap.ExpandAll();
    }
    void LoadChildNodes(TreeNode _ParentNode) 
    {
        var _ChildNodes = SiteFox.Business.Manager.SiteMapNode.GetByParentNode(Convert.ToInt32(_ParentNode.Value));
        for (int i = 0; i < _ChildNodes.Count; i++) 
        {
            var _ChildNode = _ChildNodes[i];
            TreeNode _tn = new TreeNode(_ChildNode.Title, _ChildNode.ID.ToString());
            this.LoadChildNodes(_tn);
            _ParentNode.ChildNodes.Add(_tn);
        }
    }
    void ResetSiteMapNodeAddOrEdit()
    {
        lab_SiteMapNode_AddOrEdit_Title.Text = "";
        lib_SiteMapNode_AddOrEdit_Ok.Text = "";
        ucc_SiteMapNode_AddOrEdit_Text.Value = "";
        ucc_SiteMapNode_AddOrEdit_Link.Value = "";
        ucc_SiteMapNode_AddOrEdit_ToolTip.Value = "";
        ucc_SiteMapNode_AddOrEdit_ImagePath.Value = "";
        ucc_SiteMapNode_AddOrEdit_Ordering.Value = "";
    }
    #endregion
}